import os
import numpy as np
import sys
from PyQt5.QtCore import pyqtSignal,pyqtSlot,QThread
from PyQt5.QtWidgets import QMainWindow,QApplication,QInputDialog,QFileDialog,\
    QMessageBox,QColorDialog,QListWidgetItem,QTableWidgetItem,QProgressBar,\
    QRadioButton,QMdiSubWindow,QWidget,QMenu

from PyQt5.QtCore import QPoint
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
import matplotlib.pyplot as plt
from PyQt5.uic import loadUi

class Form(QWidget):
    def __init__(self):
        super(Form, self).__init__()
        loadUi('seaclutter.ui', self)
        self.figure=plt.figure()
        self.canvas = FigureCanvasQTAgg(self.figure)
        self.horizontalLayoutSeaClutter.addWidget(self.canvas)
        self.canvas.draw()
        font = {'family': 'SimSun'}
        plt.rc('font', **font)  # 步骤一（设置字体的更多属性）
        plt.rc('axes', unicode_minus=False)

    @pyqtSlot()
    def on_pushButtonCalculateAngle_clicked(self):
        self.figure.clear()
        ax = self.figure.add_subplot()
        altitude=float(self.lineEditAltitude.text())/1e3
        minDistance=float(self.lineEditMinDistance.text())
        maxDistance=float(self.lineEditMaxDistance.text())
        distance=np.arange(minDistance,maxDistance+1,1)
        angle=np.arctan(altitude/distance)*180/np.pi
        ax.plot(distance,angle,label='不考虑地球半径')
        r=6378.
        theta=distance/r
        r1=r + altitude
        x=r1*np.sin(theta)
        y=r1*np.cos(theta)-r
        angle=np.arctan2(y,x)*180/np.pi
        ax.plot(distance,angle,label='真实地球半径')
        r=6378.*4/3
        theta=distance/r
        r1=r + altitude
        x=r1*np.sin(theta)
        y=r1*np.cos(theta)-r
        angle=np.arctan2(y,x)*180/np.pi
        ax.set_xlabel('Distance/km')
        ax.set_ylabel('Angle/degree')
        ax.plot(distance,angle,label='4/3地球半径')
        ax.grid(True)
        ax.legend()
        self.canvas.draw()


    @pyqtSlot()
    def on_pushButtonSeaClutter_clicked(self):
        altitude=float(self.lineEditAltitude.text())/1e3
        minDistance=float(self.lineEditMinDistance.text())
        maxDistance=float(self.lineEditMaxDistance.text())
        seaState=float(self.lineEditSeaState.text())
        grazingAngle=float(self.lineEditGrazingAngle.text())
        pulseWidth=float(self.lineEditPulseWidth.text())
        beamWidth=float(self.lineEditBeamWidth.text())
        distance=float(self.lineEditDistance.text())
        frequency=float(self.lineEditFrequency.text())
        c1hh=-73
        c2hh=20.78
        c3hh=7.351
        c4hh=25.65
        c5hh=0.0054
        c1vv=-50.79
        c2vv=25.93
        c3vv=0.7093
        c4vv=21.58
        c5vv=0.00211
        grazingAngle=np.linspace(.2,30,150)
        sigma0hh = c1hh + c2hh * np.log10(np.sin(grazingAngle * np.pi / 180)) + (27.5 + c3hh * grazingAngle) * np.log10(frequency) / (1 + 0.95 * grazingAngle) + c4hh * (1 + seaState) ** (1 / (2 + 0.085 * grazingAngle + 0.033 * seaState)) + c5hh * grazingAngle ** 2
        sigma0vv = c1vv + c2vv * np.log10(np.sin(grazingAngle * np.pi / 180)) + (27.5 + c3vv * grazingAngle) * np.log10(frequency) / (1 + 0.95 * grazingAngle) + c4vv * (1 + seaState) ** (1 / (2 + 0.085 * grazingAngle + 0.033 * seaState)) + c5vv * grazingAngle ** 2
        self.figure.clear()
        ax = self.figure.add_subplot()
        ax.set_xlabel('擦地角/degree')
        ax.set_ylabel('散射系数/dB')
        ax.plot(grazingAngle, sigma0hh, label='HH')
        ax.plot(grazingAngle, sigma0vv, label='VV')
        ax.grid(True)
        ax.legend()
        self.canvas.draw()

    @pyqtSlot()
    def on_pushButtonSeaClutterFrequency_clicked(self):
        altitude=float(self.lineEditAltitude.text())/1e3
        minDistance=float(self.lineEditMinDistance.text())
        maxDistance=float(self.lineEditMaxDistance.text())
        seaState=float(self.lineEditSeaState.text())
        grazingAngle=float(self.lineEditGrazingAngle.text())
        pulseWidth=float(self.lineEditPulseWidth.text())
        beamWidth=float(self.lineEditBeamWidth.text())
        distance=float(self.lineEditDistance.text())
        frequency=float(self.lineEditFrequency.text())
        c1hh=-73
        c2hh=20.78
        c3hh=7.351
        c4hh=25.65
        c5hh=0.0054
        c1vv=-50.79
        c2vv=25.93
        c3vv=0.7093
        c4vv=21.58
        c5vv=0.00211
        frequency=np.linspace(.2,30,150)
        sigma0hh = c1hh + c2hh * np.log10(np.sin(grazingAngle * np.pi / 180)) + (27.5 + c3hh * grazingAngle) * np.log10(frequency) / (1 + 0.95 * grazingAngle) + c4hh * (1 + seaState) ** (1 / (2 + 0.085 * grazingAngle + 0.033 * seaState)) + c5hh * grazingAngle ** 2
        sigma0vv = c1vv + c2vv * np.log10(np.sin(grazingAngle * np.pi / 180)) + (27.5 + c3vv * grazingAngle) * np.log10(frequency) / (1 + 0.95 * grazingAngle) + c4vv * (1 + seaState) ** (1 / (2 + 0.085 * grazingAngle + 0.033 * seaState)) + c5vv * grazingAngle ** 2
        self.figure.clear()
        ax = self.figure.add_subplot()
        ax.set_xlabel('频率/GHz')
        ax.set_ylabel('散射系数/dB')
        ax.plot(frequency, sigma0hh, label='HH')
        ax.plot(frequency, sigma0vv, label='VV')
        ax.grid(True)
        ax.legend()
        self.canvas.draw()

    @pyqtSlot()
    def on_pushButtonSeaClutterRCS_clicked(self):
        altitude=float(self.lineEditAltitude.text())/1e3
        minDistance=float(self.lineEditMinDistance.text())
        maxDistance=float(self.lineEditMaxDistance.text())
        seaState=float(self.lineEditSeaState.text())
        grazingAngle=float(self.lineEditGrazingAngle.text())
        pulseWidth=float(self.lineEditPulseWidth.text())
        beamWidth=float(self.lineEditBeamWidth.text())
        distance=float(self.lineEditDistance.text())
        frequency=float(self.lineEditFrequency.text())
        c1hh=-73
        c2hh=20.78
        c3hh=7.351
        c4hh=25.65
        c5hh=0.0054
        c1vv=-50.79
        c2vv=25.93
        c3vv=0.7093
        c4vv=21.58
        c5vv=0.00211
        grazingAngle=np.linspace(.2,30,150)
        sigma0hh = c1hh + c2hh * np.log10(np.sin(grazingAngle * np.pi / 180)) + (27.5 + c3hh * grazingAngle) * np.log10(frequency) / (1 + 0.95 * grazingAngle) + c4hh * (1 + seaState) ** (1 / (2 + 0.085 * grazingAngle + 0.033 * seaState)) + c5hh * grazingAngle ** 2
        sigma0vv = c1vv + c2vv * np.log10(np.sin(grazingAngle * np.pi / 180)) + (27.5 + c3vv * grazingAngle) * np.log10(frequency) / (1 + 0.95 * grazingAngle) + c4vv * (1 + seaState) ** (1 / (2 + 0.085 * grazingAngle + 0.033 * seaState)) + c5vv * grazingAngle ** 2
        c=3e8
        sigmahh=10*np.log10(10**(sigma0hh/10)*distance*1e3*beamWidth*np.pi/180/2*c*pulseWidth*1e-6)
        sigmavv=10*np.log10(10**(sigma0vv/10)*distance*1e3*beamWidth*np.pi/180/2*c*pulseWidth*1e-6)
        self.figure.clear()
        ax = self.figure.add_subplot()
        ax.set_xlabel('擦地角/degree')
        ax.set_ylabel('RCS/dBsm')
        ax.plot(grazingAngle,sigmahh,label='HH')
        ax.plot(grazingAngle,sigmavv,label='VV')
        ax.grid(True)
        ax.legend()
        self.canvas.draw()


    @pyqtSlot()
    def on_pushButtonSeaClutterRCSDistance_clicked(self):
        altitude=float(self.lineEditAltitude.text())/1e3
        minDistance=float(self.lineEditMinDistance.text())
        maxDistance=float(self.lineEditMaxDistance.text())
        seaState=float(self.lineEditSeaState.text())
        grazingAngle=float(self.lineEditGrazingAngle.text())
        pulseWidth=float(self.lineEditPulseWidth.text())
        beamWidth=float(self.lineEditBeamWidth.text())
        distance=float(self.lineEditDistance.text())
        frequency=float(self.lineEditFrequency.text())
        c1hh=-73
        c2hh=20.78
        c3hh=7.351
        c4hh=25.65
        c5hh=0.0054
        c1vv=-50.79
        c2vv=25.93
        c3vv=0.7093
        c4vv=21.58
        c5vv=0.00211
        distance=np.arange(minDistance,maxDistance+1,1)
        grazingAngle=np.arctan(altitude/distance)
        R=np.sqrt(distance**2+altitude**2)
        sigma0hh = c1hh + c2hh * np.log10(np.sin(grazingAngle * np.pi / 180)) + (27.5 + c3hh * grazingAngle) * np.log10(frequency) / (1 + 0.95 * grazingAngle) + c4hh * (1 + seaState) ** (1 / (2 + 0.085 * grazingAngle + 0.033 * seaState)) + c5hh * grazingAngle ** 2
        sigma0vv = c1vv + c2vv * np.log10(np.sin(grazingAngle * np.pi / 180)) + (27.5 + c3vv * grazingAngle) * np.log10(frequency) / (1 + 0.95 * grazingAngle) + c4vv * (1 + seaState) ** (1 / (2 + 0.085 * grazingAngle + 0.033 * seaState)) + c5vv * grazingAngle ** 2
        c=3e8
        sigmahh=10*np.log10(10**(sigma0hh/10)*R*1e3*beamWidth*np.pi/180/2*c*pulseWidth*1e-6)
        sigmavv=10*np.log10(10**(sigma0vv/10)*R*1e3*beamWidth*np.pi/180/2*c*pulseWidth*1e-6)
        self.figure.clear()
        ax = self.figure.add_subplot()
        ax.set_xlabel('距离/km')
        ax.set_ylabel('RCS/dBsm')
        ax.plot(distance,sigmahh,label='HH')
        ax.plot(distance,sigmavv,label='VV')
        ax.grid(True)
        ax.legend()
        self.canvas.draw()






if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    sys.exit(app.exec_())







